package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Account  string `gorm:"type:varchar(30);not null;comment:'账号'"`
	Password string `gorm:"type:char(32);not null;comment:'密码'"`
}
type Shop struct {
	gorm.Model
	Title     string  `gorm:"type:varchar(30);not null;comment:'商品标题'"`
	Price     float32 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Num       int     `gorm:"type:int;not null;comment:'商品数量'"`
	Image     string  `gorm:"type:varchar(255);not null;comment:'商品封面上传'"`
	Status    int     `gorm:"type:int;not null;comment:'0 未上架 1 上架'"`
	ConStatus int     `gorm:"type:int;not null;comment:'0 未通过 1 已通过'"`
}

type Goods struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(30);not null;comment:'购物车标题'"`
	Price  float32 `gorm:"type:decimal(10,2);not null;comment:'购物车价格'"`
	Num    int     `gorm:"type:int;not null;comment:'购物车数量'"`
	Status int     `gorm:"type:int;not null;comment:'0 未上架 1 上架'"`
	UserId int     `gorm:"type:int;not null;comment:'用户id'"`
}

func (s *Shop) CreateShop() error {
	return DB.Debug().Create(&s).Error
}

func (g *Goods) GetGoodsCar(goodsId int) error {
	return DB.Debug().Where(goodsId).Limit(1).Find(&g).Error

}

func (s *Shop) GetShop() error {
	return DB.Debug().Where("id= ?", s.ID).Limit(1).Find(&s).Error
}
func (g *Goods) CreateShop1(DB *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}
